package com.example.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.IndexDao;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.IndexService;
import com.example.tool.MD5Utils;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    IndexDao indexDao;

    @Override
    public Result login(Map<String, Object> map) {
        // TODO 登录
        String userName = map.get("userName").toString();
        String password = MD5Utils.getMD5(map.get("password").toString().getBytes());

        List<Map<Object, String>> list = indexDao.login(userName, password);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result userInfoById(String userId) {
        // TODO 根据编号查询用户的个人信息
        List<Map<Object, String>> list = indexDao.userInfoById(Integer.parseInt(userId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result courseInfoById(String userId) {
        // TODO 根据编号查询学生用户的课程信息
        List<Map<Object, String>> list = indexDao.courseInfoById(Integer.parseInt(userId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result calculateAllUsers() {
        // TODO 计算所有用户数量
        List<Map<Object, String>> list = indexDao.calculateAllUsers();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result calculateAllUsersByRoleName() {
        // TODO 计算同一角色的用户数量
        List<Map<Object, String>> list = indexDao.calculateAllUsersByRoleName();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result calculateVecateNumber() {
        // TODO 统计请假未处理数量
        List<Map<Object, String>> list = indexDao.calculateVecateNumber();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result calculateTransferNumber() {
        // TODO 统计转班未处理数量
        List<Map<Object, String>> list = indexDao.calculateTransferNumber();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result calculateQuitNumber() {
        // TODO 统计退班未处理数量
        List<Map<Object, String>> list = indexDao.calculateQuitNumber();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result courseInfoByTeacherId(String userId) {
        // TODO 根据编号查询老师用户的课程信息
        List<Map<Object, String>> list = indexDao.courseInfoByTeacherId(Integer.parseInt(userId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result roleMenuResult(String roleName) {
        // TODO 根据角色查询权限
        List<Map<Object, String>> list = indexDao.roleMenuResult(roleName);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }
}
